#include "friend.h"
#include "client.h"
#include "ui_friend.h"
#include <QInputDialog>
#include "protocol.h" //PDU头文件
#include <QtDebug>
#include <QMessageBox>

Friend::Friend(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Friend)
{
    ui->setupUi(this);
    m_pOnlineUser = new OnlineUser;
    m_pChat = new Chat;//构造函数中初始化
    flushFriend();
}

Friend::~Friend()//析构函数中释放
{
    delete ui;
    delete m_pOnlineUser;
    delete m_pChat;
}

OnlineUser *Friend::getOnlineUser()
{
    return m_pOnlineUser;
}

Chat *Friend::getChat()
{
    return m_pChat;
}

void Friend::showOnlineFriend(QStringList FriendList)
{
    ui->friend_LW->clear();//清空
    ui->friend_LW->addItems(FriendList);//传入
}

void Friend::flushFriend()
{
    //获取当前用户名，把名字发给服务器
    QString strName = Client::getInstance().m_strLoginName;
    PDU* pdu = mkPDU(0);
    pdu->uiMsgType = ENUM_MSG_TYPE_ONLINE_FRIEND_REQUEST;
    memcpy(pdu->caData, strName.toStdString().c_str(), strName.size());
    qDebug() << "flushFriend pdu->caData" << pdu->caData;
    Client::getInstance().sendPDU(pdu);
}

void Friend::on_findUser_PB_clicked()
{
    QString name = QInputDialog::getText(this, "搜索", "用户名：");
    if(name.isEmpty()) {//判空
          return;
    }

    if(name == Client::getInstance().m_strLoginName)
    {
        QMessageBox::information(this,"查找用户","这个就是你自己");
        return;
    }

    PDU *pdu = mkPDU(0);//构建PDU
    pdu->uiMsgType = ENUM_MSG_TYPE_FIND_USER_REQUEST;//PDU类型
    memcpy(pdu->caData,name.toStdString().c_str(), name.size());//发送或者接受的数据
    qDebug() <<"on_findUser_PB_clicked pdu->caData" << pdu->caData;//打印
    Client::getInstance().sendPDU(pdu);
}

void Friend::on_onlineUser_PB_clicked()
{

    if(m_pOnlineUser->isHidden()) {//如果存在
        m_pOnlineUser->show();//展示
    }
    PDU *pdu = mkPDU(0);//构建PDU
    pdu->uiMsgType = ENUM_MSG_TYPE_ONLINE_USER_REQUEST;//PDU类型
    Client::getInstance().sendPDU(pdu);
}

void Friend::on_flushFriend_PB_clicked()
{
    flushFriend();
}

void Friend::on_delFriend_PB_clicked()//删除好友槽函数
{
    QListWidgetItem* pItem = ui->friend_LW->currentItem();
    if(!pItem){
        QMessageBox::information(this, "删除好友", "你也妹选好友啊？？");
        return;
    }
    QString tarName = pItem->text();//目标用户
    QString curName = Client::getInstance().m_strLoginName;//当前用户
    int ret = QMessageBox::question(this, "删除好友", QString("是否确认删除好友 %1?").arg(tarName));
    if(ret != QMessageBox::Yes){
        return;
    }
    PDU* pdu = mkPDU(0);//构建
    pdu->uiMsgType = ENUM_MSG_TYPE_DEL_FRIEND_REQUEST;
    memcpy(pdu->caData,tarName.toStdString().c_str(), 32);//将目标用户放进pdu 发给服务器
    memcpy(pdu->caData + 32,curName.toStdString().c_str(), 32);//将当前用户放进pdu 发给服务器
    Client::getInstance().sendPDU(pdu);
}

void Friend::on_chat_PB_clicked()
{
    QListWidgetItem* pItem = ui->friend_LW->currentItem();
    if(!pItem){
        QMessageBox::information(this, "聊天", "你也妹选好友啊？？");
        return;
    }
    if(m_pChat->isHidden()){//如果聊天界面没显示，显示出来
        m_pChat->show();
    }
    m_pChat->m_chatName = pItem->text();//增加一个聊天名字的成员变量
}
